package com.tudouji.project.product.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.product.domain.Brand;
import com.tudouji.project.product.service.IBrandService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 品牌管理Controller
 * 
 * @author wf
 * @date 2020-12-28
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController extends BaseController
{
    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌管理列表
     */
    @PreAuthorize("@ss.hasPermi('product:brand:list')")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand)
    {
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }

    /**
     * 导出品牌管理列表
     */
    @PreAuthorize("@ss.hasPermi('product:brand:export')")
    @Log(title = "品牌管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Brand brand)
    {
        List<Brand> list = brandService.selectBrandList(brand);
        ExcelUtil<Brand> util = new ExcelUtil<Brand>(Brand.class);
        return util.exportExcel(list, "brand");
    }

    /**
     * 获取品牌管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:brand:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(brandService.selectBrandById(guid));
    }

    /**
     * 新增品牌管理
     */
    @PreAuthorize("@ss.hasPermi('product:brand:add')")
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Brand brand)
    {
//        Brand b = new Brand();
//        b.setCode(brand.getCode());
//        List<Brand> brandList = this.brandService.selectBrandList(b);
//        if(brandList.size() > 1){
//            return AjaxResult.error("编码已经存在");
//        }
        return toAjax(brandService.insertBrand(brand));
    }

    /**
     * 修改品牌管理
     */
    @PreAuthorize("@ss.hasPermi('product:brand:edit')")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand)
    {
        Brand b = new Brand();
        b.setGuid(brand.getGuid());
        b.setCode(brand.getCode());
        List<Brand> brandList = this.brandService.selectBrandList(b);
        if(brandList.size() > 1){
            return AjaxResult.error("编码已经存在");
        }
        return toAjax(brandService.updateBrand(brand));
    }

    /**
     * 删除品牌管理
     */
    @PreAuthorize("@ss.hasPermi('product:brand:remove')")
    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(brandService.deleteBrandByIds(guids));
    }


    @GetMapping("/findBrandList")
    public AjaxResult findBrandList(Brand brand)
    {
        List<Brand> list = brandService.selectBrandList(brand);
        return AjaxResult.success(list);
    }
}
